package com.hbgc.controller;

import com.github.pagehelper.PageInfo;
import com.hbgc.common.PageResult;
import com.hbgc.pojo.TbType;
import com.hbgc.pojo.TbVideo;
import com.hbgc.service.IVideoService;
import com.hbgc.utils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/video")
public class VideoController {

    @Autowired
    private IVideoService iVideoService;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param searchMap
     * @return
     */
    @RequestMapping("/queryByPage")
    @ResponseBody
    public PageResult queryByPage(@RequestParam(required = false,defaultValue = "1") int page, @RequestParam(required = false,defaultValue = "10") int limit, @RequestParam Map searchMap)
    {
        PageInfo pageInfo=iVideoService.queryTbVideoByPage(page,limit,searchMap);
        return ConvertUtils.convertData(pageInfo);
    }



    /**
     * 保存数据
     * @param tbVideo
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public  PageResult save(TbVideo tbVideo)
    {

        boolean status=iVideoService.saveTbVideo(tbVideo);
        if(status)
        {
            return new PageResult(0,"保存成功");
        }
        else {
            return new PageResult(1,"保存失败");
        }
    }


    /**
     * 修改数据
     * @param tbVideo
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public  PageResult update(TbVideo tbVideo)
    {

        boolean status=iVideoService.updateTbVideoById(tbVideo);
        if(status)
        {
            return new PageResult(0,"修改成功");
        }
        else {
            return new PageResult(1,"修改失败");
        }
    }



    /**
     * 根据主键删除视频
     * @param videoid
     * @return
     */
    @RequestMapping("/remove/{videoid}")
    @ResponseBody
    public  PageResult remove(@PathVariable Long videoid)
    {

        boolean status=iVideoService.deleteTbVideoById(videoid);
        if(status)
        {
            return new PageResult(0,"删除成功");
        }
        else {
            return new PageResult(1,"删除失败");
        }
    }


    /**
     * 清空所有视频
     * @param
     * @return
     */
    @RequestMapping("/clear")
    @ResponseBody
    public  PageResult clear()
    {

        iVideoService.clear();
        return new PageResult(0,"清空成功");

    }

    /**
     * 根据主键查询视频
     * @param videoid
     * @return
     */
    @RequestMapping("/querybyid/{videoid}")
    @ResponseBody
    public  PageResult querybyid(@PathVariable Long videoid)
    {
        PageResult pageResult= new PageResult(0,"查询成功");
        TbVideo tbVideo =iVideoService.getTbVideoById(videoid);
        if(tbVideo!=null)
        {
              List data=new ArrayList<>();
              data.add(tbVideo);
              pageResult.setData(data);
        }
        else {
            pageResult.setCode(1);
            pageResult.setMsg("查询失败");
        }
        return pageResult;
    }





}
